package com.tmt.im.common.store;


import com.tmt.im.common.pojo.RedPackageSkinPoJo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.store.CacheStoreAdapter;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

import javax.cache.Cache;
import javax.cache.integration.CacheLoaderException;
import javax.cache.integration.CacheWriterException;
import java.sql.*;
import java.text.MessageFormat;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/9/2 13:17
 * @Version 1.0
 **/
@Slf4j
public class RedPackageSkinStore extends BaseCacheStoreAdapter<Long, RedPackageSkinPoJo> {

    private static final String SELECT_SQL = StoreSqlHelper.generateSqlWithParams("select", RedPackageSkinPoJo.class);
    private static final String INSERT_SQL = StoreSqlHelper.generateSqlWithParams("insert", RedPackageSkinPoJo.class);
    private static final String UPDATE_SQL = StoreSqlHelper.generateSqlWithParams("update", RedPackageSkinPoJo.class);
    private static final String DELETE_SQL = StoreSqlHelper.generateSqlWithParams("delete", RedPackageSkinPoJo.class);

    @Override
    public RedPackageSkinPoJo load(Long key) throws CacheLoaderException {
        try (Connection conn = connection()) {
            try (PreparedStatement st = conn.prepareStatement(SELECT_SQL)) {
                st.setLong(1, key);
                try(ResultSet rs = st.executeQuery()) {
                    if (rs.next()) {
                        RedPackageSkinPoJo pojo = new RedPackageSkinPoJo();
                        StoreSqlHelper.initialObject(rs, pojo);
                        return pojo;
                    }
                }
                return null;
            }
        } catch (Exception ex) {
            throw new CacheLoaderException(MessageFormat.format("红包皮肤记录(ID={0})不存在", key), ex);
        }
    }

    @Override
    public void write(Cache.Entry<? extends Long, ? extends RedPackageSkinPoJo> entry) throws CacheWriterException {
        try {
            Connection conn = connection();
            RedPackageSkinPoJo val = entry.getValue();
            try (PreparedStatement st = conn.prepareStatement(val.isInsert() ? INSERT_SQL : UPDATE_SQL)) {
                StoreSqlHelper.initPreparedStatement(st, val);
                st.executeUpdate();
            }
        } catch (Exception ex) {
            log.debug(ex.getMessage(), ex);
            throw new CacheWriterException(MessageFormat.format("保存或更新红包皮肤记录异常:{0}", ex.getMessage()), ex);
        }
    }

    @Override
    public void delete(Object key) throws CacheWriterException {
        try {
            Connection conn = connection();
            try (PreparedStatement st = conn.prepareStatement(DELETE_SQL)) {
                st.setLong(1, (Long) key);
                st.executeUpdate();
            }
        } catch (SQLException e) {
            throw new CacheWriterException("Failed to delete: " + key, e);
        }
    }

}
